package com.common.orm;

import com.common.orm.sqlparameters.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * @author Dmitry Savchenko
 */
class OrmUtils {
    
    public static void throwOutErrorExceptionMessage(String message) {
        try {
            throw new Exception(message);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public static String concateArray(Object[] objects, ArrayConcator concator) {
        StringBuilder sb = new StringBuilder(50);
        for (int i = 0; i < objects.length; i++) {
            if (i != 0) {
                sb.append(", ");
            }
            sb.append(concator.getValue(objects[i]));
        }
        return sb.toString();
    }
    
    public static interface ArrayConcator {
        
        public String getValue(Object object);
    }
    
    public static Constructor getClassConstructor(ClassData cd) {
        try {
            return cd.getClazz().getConstructor();
        } catch (NoSuchMethodException ex) {
            ex.printStackTrace();
        } catch (SecurityException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    
    public static Object createObjectInstance(Class clazz, Constructor constructor) {
        try {
            return constructor.newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
    
    public static SqlParameter getSqlParameterFromString(ClassData classData, Object object, String field) throws SQLParameterException {
        MethodColumnPair mcp = classData.getColumnsMap().get(field);
        if (mcp == null) {
            throw new SQLParameterException("Field [" + field + "] does not mapped in the table " + classData.getTableName());
        }
        
        SqlParameter parameter = null;
        try {
            String fieldType = mcp.getFieldType().getName();
            if ("int".equals(fieldType) || "java.lang.Integer".equals(fieldType)) {
                parameter = new ParameterInteger(field, (Integer) mcp.getGetter().invoke(object));
            } else if ("long".equals(fieldType) || "java.lang.Long".equals(fieldType)) {
                parameter = new ParameterLong(field, (Long) mcp.getGetter().invoke(object));
            } else if ("double".equals(fieldType) || "java.lang.Double".equals(fieldType)) {
                parameter = new ParameterDouble(field, (Double) mcp.getGetter().invoke(object));
            } else if ("float".equals(fieldType) || "java.lang.Float".equals(fieldType)) {
                parameter = new ParameterFloat(field, (Float) mcp.getGetter().invoke(object));
            } else if ("boolean".equals(fieldType) || "java.lang.Boolean".equals(fieldType)) {
                parameter = new ParameterBoolean(field, (Boolean) mcp.getGetter().invoke(object));
            } else if ("java.lang.String".equals(fieldType)) {
                parameter = new ParameterString(field, (String) mcp.getGetter().invoke(object));
            } else if ("[Ljava.lang.Integer;".equals(fieldType)) {
                parameter = new ParameterString(field, (String) integerArrayToString((Integer[])mcp.getGetter().invoke(object)));
            } else if ("java.sql.Date".equals(fieldType) || "java.util.Date".equals(fieldType)) {
                parameter = new ParameterDate(field, (Date) mcp.getGetter().invoke(object));
            } else if ("java.sql.Timestamp".equals(fieldType)) {
                parameter = new ParameterTimestamp(field, (Timestamp) mcp.getGetter().invoke(object));
            } else {
                throw new SQLParameterException("Unknown field type [" + fieldType + "]");
            }
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
        }
        return parameter;
    }
    
    private static String integerArrayToString(Integer[] array) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Integer i : array) {
            if (!first) {
                sb.append(",");
            }
            sb.append(i);
            first=false;
        }
        return sb.toString();
    }
    
    public static Integer[] stringToIntegerArray(String str) {
        if (str.length() == 0) {
            return new Integer[0];
        }
        String[] strs = str.split(",");
        ArrayList<Integer> ints = new ArrayList<Integer>();
        for (String s : strs) {
            if (s != null && !s.isEmpty()) {
                try {
                    ints.add(Integer.parseInt(s));
                } catch (Exception ex) {
                }
            }
        }
        return ints.toArray(new Integer[0]);
    }
}
